package br.com.cast.service;

import java.util.Iterator;

import javax.transaction.Transactional;

import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.IModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import br.com.cast.dao.NotaFiscalDAO;
import br.com.cast.exception.NotaFiscalCadastrada;
import br.com.cast.modelo.NotaFiscal;

@Component
public class NotaFicalService implements INotaFiscalService {

	@Autowired
	private NotaFiscalDAO notaFiscalDAO;

	@Override
	public Iterator<? extends NotaFiscal> iterator(long inicio, long count) {

		return notaFiscalDAO.consultaPaginada(inicio, count);
	}

	@Override
	public long size() {
		
		return notaFiscalDAO.total(NotaFiscal.class);
	}

	@Override
	public IModel<NotaFiscal> model(NotaFiscal notaFiscal) {
		return new CompoundPropertyModel<>(notaFiscal);
	}

	@Override
	public void detach() {
		// TODO Auto-generated method stub

	}

	@Override
	@Transactional
	public void salvar(NotaFiscal notaFiscal) {

		if (notaFiscalDAO.notaFicalCadastrada(notaFiscal)) {
			throw new NotaFiscalCadastrada();
		} else {
			if (notaFiscal.getId() == null) {
				notaFiscalDAO.salvar(notaFiscal);
			} else {
				notaFiscalDAO.alterar(notaFiscal);
			}
		}
	}

	@Override
	@Transactional
	public void excluir(NotaFiscal notaFiscal) {
		notaFiscalDAO.excluir(notaFiscal);
	}

}
